package com.fengwk.support.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.fengwk.support.function.Void;
import com.fengwk.support.util.LoggerUtils;
import com.fengwk.support.util.LoggerUtils.Logger;

/**
 * CyclicBarrier工作包装
 * 
 * @author fengwk
 *
 */
public class CyclicBarrierWork implements Runnable {

	private static final Logger LOG = LoggerUtils.getLogger(CyclicBarrierWork.class);
	
	private Void.Params0 beforeFun;
	private Void.Params0 afterFun;
	private CyclicBarrier cb;
	
	public CyclicBarrierWork() {}
	
	public CyclicBarrierWork(Void.Params0 beforeFun, Void.Params0 afterFun) {
		this.beforeFun = beforeFun;
		this.afterFun = afterFun;
	}
	
	@Override
	public void run() {
		try {
			if (beforeFun != null) {
				beforeFun.apply();
			}
			cb.await();
			if (afterFun != null) {
				afterFun.apply();
			}
		} catch (InterruptedException | BrokenBarrierException e) {
		    LOG.error(e);
		}
	}

	public CyclicBarrier getCb() {
		return cb;
	}

	public void setCb(CyclicBarrier cb) {
		this.cb = cb;
	}

}
